package com.new1cloud.file.upload.client.bootstrap;

import com.new1cloud.file.domain.UploadProgressListener;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.*;

@Setter
public class MultipleFileUploadClient {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 服务端地址
     */
    private String serverAddress;
    /**
     * 服务端口
     */
    private int port;

    /**
     * 待发送文件的绝对路径(支持多个文件的发送)
     */
    private List<String> sourceFilePaths;

    /**
     * 上传的远端目录
     */
    private String uploadDir;

    /**
     * 并行数
     */
    private Integer parallelNum;
    /**
     * 线程池
     */
    private ExecutorService executorService;

    /**
     * 上传进度监听器
     */
    private UploadProgressListener uploadProgressListener;
    /**
     * 计数器
     */
    private CountDownLatch latch;

    /**
     * 本次上传启动的客户端个数
     */
    private Integer clientThreadCount;

    /**
     * 用于启动客户端的线程池
     */
    private ExecutorService clientExecutorService;

    public MultipleFileUploadClient(String serverAddress, int port, List<String> sourceFilePaths,
                                    String uploadDir, Boolean parallel, Integer parallelNum,
                                    Integer clientThreadCount){
        this.serverAddress = serverAddress;
        this.port = port;
        this.sourceFilePaths = sourceFilePaths;
        this.uploadDir = uploadDir;
        this.parallelNum = parallelNum > 1 ? parallelNum : 1;
        if(parallel) {
            this.executorService = new ThreadPoolExecutor(1, parallelNum, 0,
                    TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1));
        }

        this.clientThreadCount = clientThreadCount > 0 ? clientThreadCount : 1;
        this.latch = new CountDownLatch(this.clientThreadCount);
        this.clientExecutorService = Executors.newFixedThreadPool(this.clientThreadCount);
    }

    public void parallelRun() {
        for(int i=0; i<this.clientThreadCount; i++) {
//            this.clientExecutorService.submit(() -> {
//
//            })
        }
    }
}
